package com.lu.pojo.thread.syn;

import java.util.ArrayList;

/**
 * @author luzhenfang
 * @date 2020 05 24
 * @description 同步代码块 来保证集合安全
 */

public class TestList {
    public static void main(String[] args) throws InterruptedException {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                // 锁
                synchronized(list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        Thread.sleep(1000);
        System.out.println(list.size());
    }
}
